package com.helpyoutimely.home;/**
 * ${PROJET_NAME}
 * Created by lixinkun on 16/4/13 15:10.
 * Email EricLi1235@gmail.com.
 */

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.helpyoutimely.R;
import com.helpyoutimely.order.OrderActivity;

/**
 * User: lixinkun
 * Date: 2016-04-13
 * Time: 15:10
 * FIXME
 */
public class MainActivity extends Activity implements View.OnClickListener{

    LinearLayout placeOrder,finish,myorderlist;
    RelativeLayout refresh;
    Button upBtn,downBtn;
    EditText priceEdit,myQuestion;
    ListView searchList;

    private String[] searchData1 = {"孩子和我吵架","孩子突然不想上学","孩子喜欢在地上打滚"};
    private String[] searchData2 = {"孩子不爱干净","孩子偷跑去网吧","孩子玩亚索打野"};

    ArrayAdapter<String> adapter1;
    ArrayAdapter<String> adapter2;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        placeOrder = (LinearLayout)findViewById(R.id.placeorder);
        finish = (LinearLayout)findViewById(R.id.finish);
        myorderlist = (LinearLayout)findViewById(R.id.myorderlist);

        refresh = (RelativeLayout) findViewById(R.id.refresh);

        upBtn = (Button) findViewById(R.id.upbtn);
        downBtn = (Button) findViewById(R.id.downbtn);

        priceEdit = (EditText) findViewById(R.id.priceedit);
        myQuestion = (EditText) findViewById(R.id.myquestion);

        placeOrder.setOnClickListener(this);
        finish.setOnClickListener(this);
        refresh.setOnClickListener(this);
        upBtn.setOnClickListener(this);
        downBtn.setOnClickListener(this);
        myorderlist.setOnClickListener(this);

        adapter1 = new ArrayAdapter<String>(
                MainActivity.this,R.layout.list_item,searchData1);
        searchList = (ListView)findViewById(R.id.searchlist);
        searchList.setAdapter(adapter1);
        adapter2 = new ArrayAdapter<String>(
                MainActivity.this,R.layout.list_item,searchData2);

        searchList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String question = (String) searchList.getAdapter().getItem(position);
                myQuestion.setText(question);
            }
        });



    }
    @Override
    public void onClick(View view){
        switch (view.getId()){
            case R.id.placeorder:
                if (!myQuestion.getText().toString().equals("")){
                    if (priceEdit.getText().toString().length() > 0 && Float.parseFloat(priceEdit.getText().toString()) >= 2){
                        Intent intent = new Intent(MainActivity.this,OrderActivity.class);
                        intent.putExtra("price",priceEdit.getText().toString());
                        intent.putExtra("question",myQuestion.getText().toString());
                        startActivity(intent);
                    } else {
                        priceEdit.setFocusable(true);
                        Toast.makeText(this,"请至少输入2元以上的价格。",Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(this,"你还没有提出问题呢。",Toast.LENGTH_SHORT);
                }

                break;
            case R.id.upbtn:
                priceEdit.setText(Float.parseFloat(priceEdit.getText().toString()) + 0.5 + "");
                break;
            case R.id.downbtn:
                if (Float.parseFloat(priceEdit.getText().toString()) >= 2){
                    priceEdit.setText(Float.parseFloat(priceEdit.getText().toString()) - 0.5 + "");
                } else {
                    Toast.makeText(this,"请至少输入2元以上的价格。",Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.finish:
                finish();
                break;
            case R.id.refresh:
                if (searchList.getAdapter() == adapter1){
                    searchList.setAdapter(adapter2);
                } else {
                    searchList.setAdapter(adapter1);
                }
                break;
            case R.id.myorderlist:
                Intent intent = new Intent(MainActivity.this,OrderListActivity.class);
                startActivity(intent);
                break;

        }
    }
}
